#include "Socket.hpp"
#include "Protool.hpp"
#include "Common.hpp"
#include <memory>
#include <iostream>
#include <string>

using namespace SocketModule;

void Usage(std::string proc)
{
    std::cerr << "Usage:" << proc << " port" << std::endl;
}

void GetDate(int& x, int& y, char& oper)
{
    std::cout << "please input x:";
    std::cin >> x;
    std::cout << "please input y:";
    std::cin >> y;
    std::cout << "please input oper:";
    std::cin >> oper;
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[1]);
        exit(USAGE_ERR);
    }

    std::string server_ip = argv[1];
    uint16_t port = std::stoi(argv[2]);

    // 创建套接字
    std::shared_ptr<Socket> client = std::make_shared<TcpSocket>();
    client->BuildTcpClientMethod();
    // 进行服务器的连接
    client->Connect(server_ip, port);

    std::unique_ptr<Protocol> protocol=std::make_unique<Protocol>();
    std::string resp_buffer;
    while (1)
    {
        int x, y;
        char oper;
        GetDate(x, y, oper);

        //构建序列化数据
        std::string req_str=protocol->BuildRequeString(x,y,oper);

        // 向服务器当中发送数据
        client->Send(req_str);
        

        // 从服务器当中获取对应的报文
        Response ret;
        bool res=protocol->GetResponse(client,resp_buffer,&ret);
        if(res==false)
            break;

        // 显示结果
        ret.ShowResult();
    }

    return 0;
}